package cn.wolfcode.wolf2w.controller;


import cn.wolfcode.wolf2w.JsonResult.JsonResult;
import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.cache.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("strategies")
public class StrategyController {
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @GetMapping("/query")
    public Object query(StrategyQuery qo){
        return JsonResult.success(strategyService.query(qo));
    }

    @GetMapping("/themes")
    public Object themes(){
        List<StrategyTheme> list = strategyThemeService.list();
        return JsonResult.success(list);
    }

    @GetMapping("/detail")
    public Object detail(String id){
        strategyStatisVORedisService.viewnumIncrease(id,1);
        Strategy strategy = strategyService.get(id);
        return JsonResult.success(strategy);
    }
    //评论添加
    @PostMapping("/addComment")
    @RequireLogin
    public Object addComment(StrategyComment comment,@UserParam UserInfo userInfo){

        //设置用户信息
        //将源对象的属性拷贝到目标对象中（前提：属性名词必须一样才可以拷贝）
        BeanUtils.copyProperties(userInfo,comment);
        //用户id属性不一样，无法设置
        comment.setUserId(userInfo.getId());
        strategyCommentService.save(comment);
        //评论数加1
        strategyStatisVORedisService.replynumIncrease(comment.getStrategyId(),1);
        return JsonResult.success();
    }
    //分页评论
    @GetMapping("/comments")
    public Object comments(StrategyCommentQuery qo){
        return JsonResult.success(strategyCommentService.query(qo));
    }
    //评论点赞功能
    @RequireLogin
    @PostMapping("/commentThumb")
    public Object commentThumb(String cid,@UserParam UserInfo userInfo){
        strategyCommentService.commentThumb(cid,userInfo.getId());
        return JsonResult.success();
    }
    //获取VO对象
    @GetMapping("/statisVo")
    public Object statisVo(String sid){
            return JsonResult.success(strategyStatisVORedisService.getStrategyStatisVO(sid));
    }
    //攻略收藏
    @RequireLogin
    @PostMapping("/favor")
    public Object favor(String sid,@UserParam UserInfo userInfo){
        boolean ret=strategyStatisVORedisService.favor(sid,userInfo.getId());
        return JsonResult.success(ret);
    }

    //顶
    @RequireLogin
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String sid,@UserParam UserInfo userInfo){
        boolean ret=strategyStatisVORedisService.strategyThumbup(sid,userInfo.getId());
        return JsonResult.success(ret);
    }
}
